/**
 * File Name	: Repetitive.h
 * Location		: /Data/
 * Lib.Name		: Data.a
 * Designer		: OS (12-09-06)
 */

#pragma once

#include "DataField.h"

namespace Core
{
	template <class T>
	class Repetitive : public DataField, public std::vector<T>
	{
	public:
		Repetitive()
		{
			Reset();
		}

		/**
		 * Destruktor
		 */
		virtual ~Repetitive()
		{
		}

		void Init()
		{
			for(typename std::vector<T>::iterator p=std::vector<T>::begin(); p!=std::vector<T>::end(); p++)
			{
				p->Init();
			}
		}

		void Reset()
		{
			std::vector<T>::clear();
		}

		/**
		 * Mengartikan Raw Data menjadi kelas entitas
		 * @param p_Data Raw Data yang akan diartikan
		 */
		virtual void ReadRawData(Core::RawData& p_Data)
		{
			unsigned char t_Size = p_Data.ReadChar();
			for(unsigned int i=0; i<t_Size; i++)
			{
				T t_T;
				t_T.ReadRawData(p_Data);
				push_back(t_T);
			}
			Init();
		}

		/**
		 * Menuliskan entitas ke Raw Data
		 * @param p_Data Raw Data tempat entitas akan menuliskan
		 */
		virtual void WriteRawData(Core::RawData& p_Data)
		{
			Init();
			p_Data.WriteChar((char) std::vector<T>::size());
			for(unsigned int i=0; i<std::vector<T>::size(); i++)
			{
				std::vector<T>::operator[](i).WriteRawData(p_Data);
			}
		}

		virtual void ReadJSON(Json::Value& pValue) {
            unsigned int tSize = pValue.size();
            for (unsigned int i = 0 ; i < tSize ; i++) {
                T t;
                t.ReadJSON(pValue[i]);
                push_back(t);
            }
            Init();
		}

		virtual void WriteJSON(Json::Value& pValue) {
		    Init();
		    for(unsigned int i=0; i<std::vector<T>::size(); i++)
			{
				std::vector<T>::operator[](i).WriteJSON(pValue[i]);
			}
		}
	};
}
